from django.db import models


# Create your models here.
class Comment(models.Model):
    user_id = models.IntegerField()
    content = models.CharField(max_length=200)
    create_date = models.DateTimeField(auto_now_add=True)
    to_user_id = models.IntegerField(null=True)


class Video(models.Model):
    video_name = models.CharField(max_length=200)
    video_cover = models.CharField(max_length=200)
    video_type_des = models.CharField(max_length=200)
    video_rank = models.FloatField()
    video_comments = models.ManyToManyField(Comment, through='VideoComment')
    create_date = models.DateTimeField(auto_now_add=True)
    update_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.video_name


class DubanUser(models.Model):
    user_name = models.CharField(max_length=200)
    mobile_phone = models.CharField(max_length=20)
    password = models.CharField(max_length=20)
    session_token = models.CharField(max_length=200)
    email_address = models.CharField(max_length=200)
    create_date = models.DateTimeField(auto_now_add=True)
    update_date = models.DateTimeField(auto_now=True)
    avatar = models.ImageField(upload_to='avatar', null=True)
    like_videos = models.ManyToManyField(Video, through='UserLikeVideo')
    like_article = models.ManyToManyField('Article', through='UserLikeArticle')

    def __str__(self):
        return self.user_name


class UserLikeVideo(models.Model):
    user = models.ForeignKey(DubanUser, on_delete=models.CASCADE)
    video = models.ForeignKey(Video, on_delete=models.CASCADE)
    create_date = models.DateTimeField(auto_now_add=True)


class UserLikeArticle(models.Model):
    user = models.ForeignKey(DubanUser, on_delete=models.CASCADE)
    article = models.ForeignKey('Article', on_delete=models.CASCADE)
    create_date = models.DateTimeField(auto_now_add=True)


class VideoComment(models.Model):
    video = models.ForeignKey(Video, on_delete=models.CASCADE)
    comment = models.ForeignKey(Comment, on_delete=models.CASCADE)
    create_date = models.DateTimeField(auto_now_add=True)


class Article(models.Model):
    auther = models.ForeignKey('DubanUser', on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    content = models.TextField()
    images = models.ManyToManyField('Picture', through='ArticlePicture')
    article_comments = models.ManyToManyField(Comment, through='ArticleComment')
    create_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title + '---' + self.auther.user_name


class ArticleComment(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    comment = models.ForeignKey(Comment, on_delete=models.CASCADE)


class Picture(models.Model):
    image = models.ImageField(upload_to='picture')

    def __str__(self):
        return self.image.url


class ArticlePicture(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    image = models.ForeignKey(Picture, on_delete=models.CASCADE)
